package com.xxxx.spring.day03;

import com.alibaba.druid.pool.DruidDataSource;
import com.xxxx.spring.day03.component.Bean1;
import com.xxxx.spring.day03.mapper.Mapper1;
import com.xxxx.spring.day03.mapper.Mapper2;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.xxxx.spring.day03.component")
public class Config {

    @Bean
    public Bean1 bean1(){
        return new Bean1();
    }

    /**
     *  该方法上有参数 DataSource dataSource
     *  要指定自动装配
     * @param dataSource
     * @return
     */
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        return sqlSessionFactoryBean;
    }

    @Bean(initMethod = "init")
    public DruidDataSource dataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://127.0.0.1:3306/facility");
        druidDataSource.setName("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }

    // @Bean
    // public MapperFactoryBean<Mapper1> mapper1(SqlSessionFactory sqlSessionFactory){
    //     MapperFactoryBean<Mapper1> factory = new MapperFactoryBean<>(Mapper1.class);
    //     factory.setSqlSessionFactory(sqlSessionFactory);
    //     return factory;
    // }
    //
    // @Bean
    // public MapperFactoryBean<Mapper2> mapper2(SqlSessionFactory sqlSessionFactory){
    //     MapperFactoryBean<Mapper2> factory = new MapperFactoryBean<>(Mapper2.class);
    //     factory.setSqlSessionFactory(sqlSessionFactory);
    //     return factory;
    // }
}
